package com.cssw.fyzb.web.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.base.web.domain.AjaxPage;
import com.cssw.fyzb.core.dao.acct.AcctAdjustDao;
import com.cssw.fyzb.core.dao.acct.AcctDao;
import com.cssw.fyzb.core.dao.appeal.AppealDao;
import com.cssw.fyzb.core.dao.appeal.AppealTypeDao;
import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.model.dodb.*;
import com.cssw.fyzb.web.vo.AcctVo;
import com.cssw.fyzb.web.vo.AppealVo;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AcctService {
    @Autowired
    AcctAdjustDao acctAdjustDao;
    @Autowired
    UserDao userDao;
    @Autowired
    AppealTypeDao appealTypeDao;
    @Autowired
    AcctDao acctDao;

    public AjaxPage<AcctVo> list(int page, int limit, Map param) {
        Page<User> list= userDao.listUserDirectFriendByCond(param, page, limit);
        List<AcctVo> funcVos = new ArrayList<>();
        if (null != list.getList() && list.getList().size() != 0) {
            AcctVo acctVo = null;
            for (User user : list.getList()) {
                acctVo = new AcctVo();
                acctVo.setMobile(user.getMobile());
                acctVo.setUserId(user.getUserId());
                acctVo.setNickName(user.getNickName());
                acctVo.setCreateTime(user.getCreateTime());
                List<Acct> acctList = acctDao.listAcctByUserId(user.getUserId());
                for (Acct acct : acctList) {
                    if (acct.getAcctType().equals("F")) {
                        acctVo.setFBalanceFee(acct.getBalanceFee());
                    } else if (acct.getAcctType().equals("Y")) {
                        acctVo.setYBalanceFee(acct.getBalanceFee());
                    } else if (acct.getAcctType().equals("G")) {
                        acctVo.setGBalanceFee(acct.getBalanceFee());
                    }
                }
                funcVos.add(acctVo);
            }
        }

        return AjaxPage.success(funcVos,list.getTotalRow(),list.getTotalPage());
    }



    public boolean update(Long userId,String acctType,long type, BigDecimal num, Long perId,String remark){
        boolean flag = Db.tx(() -> {
            User user = userDao.getById(userId);
            if (user==null) return false;
            acctAdjustDao.addAcctLog(userId, acctType, type, num, remark);
            if(type == 1)
                acctDao.addBalance(userId, acctType, num, null, "TT105", "BT203");
            else
                acctDao.minusBalance(userId,acctType,num,null,"TT105","BT203");
            return true;
        });
        return flag;
    }

}
